<?php

namespace addons\yungift\model;

use addons\yungift\library\ModelTrait;
use addons\yungift\library\Super;
use think\Model;
use traits\model\SoftDelete;

/**
 * 礼品 - 主模型
 */
class GiftMain extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yungift_gift';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    // 规格数据
    public function giftSpecListData()
    {
        return $this->hasMany(GiftSpec::class, 'gift_id', 'id');
    }

    public function scopeShow($query)
    {
        return $query->where('status', '=', self::STATUS_NORMAL);
    }

    // 封面图
    protected function setCoverUrlAttr($value)
    {
        return str_replace(request()->domain(), '', $value);
    }

    // 封面图
    public function getCoverUrlAttr($value, $data)
    {
        return cdnurl($value, true);
    }

    protected function setStartTimeAttr($value)
    {
        return $value && ! is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setEndTimeAttr($value)
    {
        return $value && ! is_numeric($value) ? strtotime($value) : $value;
    }

    // 图片列表
    public function getPhotoListAttr($value, $data)
    {
        $photoList = [];
        if ($photos = $data['photos']) {
            $photos = explode(',', $photos);
            foreach ($photos as $item) {
                $photoList[] = cdnurl($item, true);
            }
        }

        return $photoList;
    }

    // 封面图
    public function getContentAttr($value, $data)
    {
        return str_replace('"/assets/addons/yungift/',
            '"' . cdnurl('/assets/addons/yungift/', true), $value);
    }

    // 类型
    const TYPE_1 = 1;
    const TYPE_2 = 2;

    public static function getTypeList()
    {
        return [self::TYPE_1 => __('实物'), self::TYPE_2 => __('虚拟商品')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $optionList = self::getTypeList();

        return $optionList[$data['type']] ?? $data['type'];
    }

    // 状态
    const STATUS_NORMAL = 1;
    const STATUS_HIDDEN = 2;

    public static function getStatusList()
    {
        return [self::STATUS_NORMAL => __('Normal'), self::STATUS_HIDDEN => __('屏蔽')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $optionList = self::getStatusList();

        return $optionList[$data['status']] ?? $data['status'];
    }

    protected function getSpecListAttr($value, $data)
    {
        $specs = Super::arrayGetSet($this->giftSpecListData, ['id', 'name', 'worth', 'amount', 'count_left'], true);

        return json_encode($specs);
    }

    // 获取select列表
    public static function getOptionList()
    {
        return GiftMain::column('id, name');
    }
}
